package com.burst.reptile.common.utils.textreview;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.burst.reptile.framework.web.result.ServiceResult;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 敏感词检测工具类
 * @author wujin
 *
 * @date: 2020-9-8 9:30:39
 */
@Slf4j
public class TextContent {
	
	public static final String URL = "http://ci.lingke.pro/index/index/checkText";

	public static final int[] WORDS_TYPE = new int[] {0, 1, 2, 3, 4, 5};
	
	public static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
	
	public static final String MSG_SEC_CHECK_URL = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=";
	
	public static final String MEDIA_CHECK_URL = "https://api.weixin.qq.com/wxa/media_check_async?access_token=";
	
	public static final String ACCESS_TOKEN = "37_syE3YsbQwkohxbDDxgsXb56GjriORMvnuk_33J6Jc87x9aMw8P37elpvpmr7MezHyIfBZp8Q5v1Vxhe5wt_qs1GsWn9TEsIYI-vPeRKDT07rBzBj0yu3BtwBPYWaew979amdzNv_rtTXxhKETAPfAAAPWL";
	
	/**
	 * 敏感词检测
	 * @param str 检测文本
	 * @return
	 */
	public static boolean examineText(String str) {
//		if(StrUtil.isNotEmpty(str)) {
//			Map<String, Object> paramMap = new HashMap<>();
//			paramMap.put("check_text", str);
//			paramMap.put("words_type", WORDS_TYPE.toString());
//			String resStr = HttpUtil.post(URL, paramMap);
//			JSONObject parseObject = JSONObject.parseObject(resStr);
//			if(parseObject.getInteger("error") == 0) {
//				JSONObject data = parseObject.getJSONObject("data");
//				JSONObject baiduCheck = data.getJSONObject("baidu_check");
//				Integer is_pass = baiduCheck.getInteger("is_pass");
//				if(is_pass == 0) {
//					JSONArray words = baiduCheck.getJSONArray("words");
//					JSONArray target = baiduCheck.getJSONArray("target");
//					if(words.size() > 0 || target.size() > 0) {
//						log.error("包含敏感词，{}，检测不通过，分析结果，{}", words.toString(), target.toString());
//						return true;
//					}
//				}
//			}
//		}
		return false;
	}
	
	
	/**
	 * 敏感词检测
	 * @param str 检测文本
	 * @return
	 */
	public static ServiceResult<Object> examineTextres(String str) {
//		if(StrUtil.isNotEmpty(str)) {
//			Map<String, Object> paramMap = new HashMap<>();
//			paramMap.put("check_text", str);
//			paramMap.put("words_type", WORDS_TYPE.toString());
//			String resStr = HttpUtil.post(URL, paramMap);
//			JSONObject parseObject = JSONObject.parseObject(resStr);
//			System.out.println(parseObject.toJSONString());
//			if(parseObject.getInteger("error") == 0) {
//				JSONObject data = parseObject.getJSONObject("data");
//				JSONObject baiduCheck = data.getJSONObject("baidu_check");
//				Integer is_pass = baiduCheck.getInteger("is_pass");
//				if(is_pass == 0) {
//					JSONArray words = baiduCheck.getJSONArray("words");
//					JSONArray target = baiduCheck.getJSONArray("target");
//					if(words.size() > 0 || target.size() > 0) {
//						log.error("包含敏感词，{}，检测不通过，分析结果，{}", words.toString(), target.toString());
//						return ServiceResult.error("文本检测不通过,分析结果：" + target.toString());
//					}
//				}
//			}
//		}
		return ServiceResult.success();
	}
	
	
	/**
	 * 微信珊瑚安全内容审核
	 * @param accessToken
	 * @param content
	 * @return
	 */
	public boolean msgSecCheck(String accessToken, String content) {
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("content", content);
		String resStr = HttpUtil.post(MSG_SEC_CHECK_URL + accessToken, JSONObject.toJSONString(paramMap));
		JSONObject parseObject = JSONObject.parseObject(resStr);
		Integer integer = parseObject.getInteger("errcode");
		return integer.equals(87014);
	}
	
	/**
	 * 微信珊瑚安全音频、图片审核
	 * @param accessToken 
	 * @param mediaurl 要检测的多媒体url
	 * @param mediaType 媒体类型 1:音频;2:图片
	 * @return
	 */
	public boolean mediaCheck(String accessToken, String mediaurl, Integer mediaType) {
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("media_url", mediaurl);
		paramMap.put("media_type", mediaType);
		String resStr = HttpUtil.post(MEDIA_CHECK_URL + accessToken, JSONObject.toJSONString(paramMap));
		JSONObject parseObject = JSONObject.parseObject(resStr);
		Integer integer = parseObject.getInteger("errcode");
		return integer.equals(0);
	}
	
	public static void main(String[] args) {
		String str = "";
		boolean examineText = examineText(str);
		System.out.println(examineText);
		ServiceResult<Object> examineTextres = examineTextres(str);
		System.out.println(examineTextres.getMessage());
		
//		String string = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx446b39705f516b01&secret=64286693a855ecc487f691eaa372372b");
//		System.out.println(string);
//		JSONObject parseObject = JSONObject.parseObject(string);
//		String accessToken = parseObject.getString("access_token");
//		Integer expiresIn = parseObject.getInteger("expires_in");
//		System.out.println(accessToken);
//		System.out.println(expiresIn);

//		String body = "{\"content\": \"完2347全dfji试3726测asad感3847知qwez到\"}";
//		String post = HttpUtil.post(MSG_SEC_CHECK_URL + ACCESS_TOKEN, body);
//		System.out.println(post);
//		
//		String imgurl = "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3103179322,2399044469&fm=26&gp=0.jpg";
//		Map<String, Object> paramMap = new HashMap<>();
//		paramMap.put("media_url", imgurl);
//		paramMap.put("media_type", 2);
//		String post2 = HttpUtil.post(MEDIA_CHECK_URL + ACCESS_TOKEN, JSONObject.toJSONString(paramMap));
//		System.out.println(post2);
		
	}
}
